package com.example.utill;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Durid
 */
public class JDBCUtills {
    private static DataSource ds;
    /**
     * 获取连接池对象
     */
     static {
         try{
         //1.加载配置文件
        Properties pro = new Properties();
        //使用Classloader加载配置文件
             InputStream is = JDBCUtills.class.getClassLoader().getResourceAsStream("druid.properties");
             pro.load(is);
            //2.初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static DataSource getDataSource(){
        return ds;
    }
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
